package org.jeecg.jt808.Jt808Control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JT808AttributeConverter {

    private static final Map<Integer, String> attributeKeyMap = new HashMap<>();

    static {  // 静态初始化块，预先计算 key 的十六进制字符串
        attributeKeyMap.put(0x01, "mileage");
        attributeKeyMap.put(0x25, "extendedVehicleSignalStatus");
        attributeKeyMap.put(0x2A, "ioStatus");
        attributeKeyMap.put(0x2B, "analog");
        attributeKeyMap.put(0x30, "wirelessSignalStrength");
        attributeKeyMap.put(0x31, "gnssSatelliteCount");
        attributeKeyMap.put(0x60, "baseStationData");
        attributeKeyMap.put(0xE3, "battery");
        attributeKeyMap.put(0xF2, "lockStatus");
        // ... 其他 key ...
    }

    public static Map<String, Object> convertAttributes(Map<Integer, Object> attributes) {
        Map<String, Object> convertedAttributes = new HashMap<>();



        for (Map.Entry<Integer, Object> entry : attributes.entrySet()) {
            Integer key = entry.getKey();
            Object value = entry.getValue();
            String attributeName = attributeKeyMap.get(key); // 直接从 map 中获取


            if (attributeName != null) { // 避免不必要的 switch 分支
                switch (attributeName) { // 使用字符串进行 switch
                    case "mileage":
                        convertedAttributes.put(attributeName, convertMileage((Long) value));
                        break;
                    case "analog":
                        convertedAttributes.put(attributeName, convertAnalog((int) value));
                        break;
                    case "baseStationData":
                        convertedAttributes.put(attributeName, convertBaseStationData((byte[]) value));
                        break;
                    case "battery":
                        convertedAttributes.put(attributeName, convertBattery((byte[]) value));
                        break;
                    case "lockStatus":
                        convertedAttributes.put(attributeName, convertLockStatus((int) value));
                        break;
                    // ... other cases ...
                    default:
                        convertedAttributes.put(attributeName, value); // 其他已知 key，无需转换
                        break;

                }
            } else {
                convertedAttributes.put(String.format("0x%02X", key), value); //  未知 key，使用十六进制字符串表示
            }
        }

        return convertedAttributes;
    }
    /**
     * 解析锁状态信息.
     *
     * @param lockStatus 锁状态的整数值.
     * @return 包含各个锁状态的 Map.
     */
    private static Map<String, Boolean> convertLockStatus(int lockStatus) {
        Map<String, Boolean> lockStatusInfo = new HashMap<>();

        // 解析座桶锁状态
        lockStatusInfo.put("seatLockOpen", (lockStatus & 0x01) == 0x01);
        // 解析电池仓锁状态
        lockStatusInfo.put("batteryLockOpen", (lockStatus & 0x02) == 0x02);
        // 解析头盔锁状态
        lockStatusInfo.put("helmetLockOpen", (lockStatus & 0x04) == 0x04);

        return lockStatusInfo;
    }

    private static double convertMileage(Long value) {
        return value * 0.1; // 里程单位转换为 km
    }

    /**
     * 解析基站数据
     * 格式：
     * - 第一个字节表示基站个数
     * - 每个基站数据为 10 个字节：
     *   - 0-1 字节：MCC（移动国家码）
     *   - 2 字节：MNC（移动网络码）
     *   - 3-4 字节：LAC（位置区码）
     *   - 5-8 字节：Cell ID（基站标识符）
     *   - 9 字节：信号强度
     * @param data 基站数据的字节数组
     * @return 解析后的基站数据列表
     */
    private static List<Map<String, Object>> convertBaseStationData(byte[] data) {
        List<Map<String, Object>> baseStationList = new ArrayList<>();

        // 第一个字节表示基站个数
        int stationCount = data[0] & 0xFF;

        // 每个基站数据为 10 个字节，基站数据从 data[1] 开始
        int offset = 1;
        for (int i = 0; i < stationCount; i++) {
            if (offset + 9 < data.length) {
                Map<String, Object> baseStationInfo = new HashMap<>();

                int mcc = ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF); // MCC
                int mnc = data[offset + 2] & 0xFF; // MNC
                int lac = ((data[offset + 3] & 0xFF) << 8) | (data[offset + 4] & 0xFF); // LAC
                int cellId = ((data[offset + 5] & 0xFF) << 24) | ((data[offset + 6] & 0xFF) << 16)
                        | ((data[offset + 7] & 0xFF) << 8) | (data[offset + 8] & 0xFF); // Cell ID
                int signalStrength = data[offset + 9] & 0xFF; // 信号强度

                baseStationInfo.put("MCC", mcc);
                baseStationInfo.put("MNC", mnc);
                baseStationInfo.put("LAC", lac);
                baseStationInfo.put("CellID", cellId);
                baseStationInfo.put("SignalStrength", signalStrength);

                baseStationList.add(baseStationInfo);
                offset += 10; // 每个基站数据占 10 个字节
            } else {
                throw new IllegalArgumentException("基站数据长度不足，无法解析");
            }
        }

        return baseStationList;
    }


    private static Map<String, Double> convertAnalog(int value) {
        Map<String, Double> analogValues = new HashMap<>();
        analogValues.put("AD0", (value & 0xFFFF) * 0.001); // AD0 的值，转换为伏特
        analogValues.put("AD1", ((value >> 16) & 0xFFFF) * 0.001); // AD1 的值，转换为伏特
        return analogValues;
    }


    private static Map<String, Integer> convertBattery(byte[] data) {
        Map<String, Integer> batteryInfo = new HashMap<>();
        int batteryLevel = ((data[0] & 0xFF) << 8) | (data[1] & 0xFF);
        int batteryVoltage = ((data[2] & 0xFF) << 8) | (data[3] & 0xFF);
        int chargingVoltage = ((data[4] & 0xFF) << 8) | (data[5] & 0xFF);

        batteryInfo.put("level", batteryLevel);
        batteryInfo.put("voltage", batteryVoltage);
        batteryInfo.put("chargingVoltage", chargingVoltage);
        return batteryInfo;
    }



    public static void main(String[] args) {
        Map<Integer, Object> attributes = new HashMap<>();
        attributes.put(0x01, 2500); // mileage
        attributes.put(0x25, 0xFF);   // extendedVehicleSignalStatus
        attributes.put(0x2A, 0x03);   // ioStatus
        attributes.put(0x2B, 0x12345678); // analog
        attributes.put(0x30, 50);     // wirelessSignalStrength
        attributes.put(0x31, 10);     // gnssSatelliteCount
        attributes.put(0x60, new byte[]{2, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00, 0x0A}); // baseStationData
        byte[] data = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
        attributes.put(0xE3, data);   // battery
        attributes.put(0xF2, 0x01);   // lockStatus

        Map<String, Object> convertedAttributes = convertAttributes(attributes);
        System.out.println(convertedAttributes);
    }
}
